<?php



// 
// session_start();

class Auth{
    // Parse error: syntax error, unexpected variable "$sessionKey", expecting "function" or "const" in D:\wamp\www\part18\models\auth.php on line 9
    // $sessionKey = "ksjkkjjskdjdkdjk";

    // 构造函数，在创建类实例时被调用
    // private $sessionKey;
    // public function __construct() {
    //     $this->sessionKey = "oRcWjL49Tj8xMMR44ajm";
    // }

    private $sessionKey = "oRcWjL49Tj8xMMR44ajm";

    // 传 uid 后，返回 token 
    public function generate($uid){
        // token = uid + 时间戳 + key
        $timeStamp = time();
        return array("uid" => $uid, "token" => md5("{$uid}{$timeStamp}{$this->sessionKey}"));
        // return md5("{$uid}{$timeStamp}{$this->sessionKey}");
    }

    // 验证访问者的身份（不能修改别人的数据）
    public function verify($uid, $token){
        // if($_SESSION[$uid] == $token){ // error
        // if(isset($_SESSION[$uid]) && $_SESSION[$uid] == $token){ // error
        if(isset($_SESSION["u{$uid}"]) && $_SESSION["u{$uid}"] == $token){
            return true;
        }else{
            return false;
        }
    }
};

